package ph.com.gs3.formalistics.custom.implementation;

import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.formalistics.custom.interfaces.CustomDocumentActivity;
import ph.com.gs3.formalistics.model.valueobjects.business.form.Form;
import ph.com.gs3.formalistics.model.valueobjects.business.form.FormField.FieldType;
import ph.com.gs3.formalistics.presenter.DocumentActivity;
import ph.com.gs3.formalistics.view.valueobjects.parsers.DateUtilities;
import android.os.AsyncTask;
import android.widget.Toast;

public class SmartMaticEquipmentScanning implements CustomDocumentActivity {

	public static final String PCOS_MACHINE_FORM_ID = "324";
	public static final String PCOS_MACHINE_FORM_SUBMIT_ACTION = "Submit";

	private List<Equipment> dummyEquipments = new ArrayList<>();

	private DocumentActivity sourceActivity;

	public SmartMaticEquipmentScanning(DocumentActivity sourceActivity) {
		this.sourceActivity = sourceActivity;

		dummyEquipments = initializeDummyEquipments();
	}

	@Override
	public void onBarcodeScanned(String fieldName, String scannedCode) {

		Form currentForm = sourceActivity.getCurrentForm();

		if (PCOS_MACHINE_FORM_ID.equals(currentForm.getWebId())) {
			boolean codeValid = false;
			Equipment scannedEquipment = null;

			for (Equipment availableEquipment : dummyEquipments) {
				if (availableEquipment.getSerialNumber().equals(scannedCode)) {
					codeValid = true;
					scannedEquipment = availableEquipment;
					break;
				}
			}

			String message = codeValid ? "Equipment found" : "Equipment not found";

			Toast.makeText(sourceActivity, message, Toast.LENGTH_LONG).show();

			if (codeValid) {
				updateDocumentValuesWithEquipment(scannedEquipment);

				sourceActivity.submitDocumentAction(PCOS_MACHINE_FORM_SUBMIT_ACTION);

				AsyncTask<Void, Void, Void> openBlankFormTask = new AsyncTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {
						try {
							Thread.sleep(5000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return null;
					}

					@Override
					protected void onPostExecute(Void result) {
						super.onPostExecute(result);
						// sourceActivity.openNewBlankForm();
					}

				};
				openBlankFormTask.execute();
				sourceActivity.openNewBlankForm();
			}
		}

	}

	private List<Equipment> initializeDummyEquipments() {

		List<Equipment> equipments = new ArrayList<>();

		Equipment olcd1 = new Equipment();
		Equipment olcd2 = new Equipment();
		Equipment bes = new Equipment();
		Equipment acc = new Equipment();
		Equipment tp = new Equipment();

		olcd1.setSerialNumber("1234567890");
		olcd1.setProductName("Operator LCD Screen With Counter");
		olcd1.setDeviceType("Security");
		olcd1.setBatchNumber("LCD20140404");
		olcd1.setControlNumber("CTRL_LCD01295012581");

		olcd2.setSerialNumber("9771234567003");
		olcd2.setProductName("Operator LCD Screen With Counter");
		olcd2.setDeviceType("Security");
		olcd2.setBatchNumber("LCD20140404");
		olcd2.setControlNumber("CTRL_LCD01260952152");

		bes.setSerialNumber("0123456789012");
		bes.setProductName("Ballot Entry Slot");
		bes.setDeviceType("Input");
		bes.setBatchNumber("BES20120613");
		bes.setControlNumber("CTRL_BES0916205201512");

		acc.setSerialNumber("850006000012");
		acc.setProductName("Administrator Access Compartment");
		acc.setDeviceType("Input/Output");
		acc.setBatchNumber("ACC20120613");
		acc.setControlNumber("CTRL_ACC012959102521");

		tp.setSerialNumber("012345678905");
		tp.setProductName("Transmission Port");
		tp.setDeviceType("Input/Output");
		tp.setBatchNumber("TP20120613");
		tp.setControlNumber("CTRL_TP091250264789");

		equipments.add(olcd1);
		equipments.add(olcd2);
		equipments.add(bes);
		equipments.add(acc);
		equipments.add(tp);

		return equipments;

	}

	private void updateDocumentValuesWithEquipment(Equipment equipment) {

		sourceActivity.updateFieldValue(FieldType.TEXT_FIELD, "fldsrnumbarcode",
		        equipment.getSerialNumber());
		sourceActivity.updateFieldValue(FieldType.TEXT_FIELD, "fldprodname",
		        equipment.getProductName());
		sourceActivity.updateFieldValue(FieldType.TEXT_FIELD, "flddevtype",
		        equipment.getDeviceType());
		sourceActivity.updateFieldValue(FieldType.TEXT_FIELD, "textbox_16",
		        equipment.getBatchNumber());
		sourceActivity.updateFieldValue(FieldType.TEXT_FIELD, "fldcrtlnum",
		        equipment.getControlNumber());
		sourceActivity.updateFieldValue(FieldType.TEXT_FIELD, "flddate",
		        DateUtilities.getServerFormattedCurrentDate());

	}

	public static class Equipment {

		private String serialNumber;	// fldsrnumbarcode
		private String productName;	// fldprodname
		private String deviceType; // flddevtype
		private String date; // flddate
		private String controlNumber; // fldcrtlnum
		private String dimensionsGrossWeight; // flddimensions
		private String batchNumber; // textbox_16

		public String getSerialNumber() {
			return serialNumber;
		}

		public void setSerialNumber(String serialNumber) {
			this.serialNumber = serialNumber;
		}

		public String getProductName() {
			return productName;
		}

		public void setProductName(String productName) {
			this.productName = productName;
		}

		public String getDeviceType() {
			return deviceType;
		}

		public void setDeviceType(String deviceType) {
			this.deviceType = deviceType;
		}

		public String getDate() {
			return date;
		}

		public void setDate(String date) {
			this.date = date;
		}

		public String getControlNumber() {
			return controlNumber;
		}

		public void setControlNumber(String controlNumber) {
			this.controlNumber = controlNumber;
		}

		public String getDimensionsGrossWeight() {
			return dimensionsGrossWeight;
		}

		public void setDimensionsGrossWeight(String dimensionsGrossWeight) {
			this.dimensionsGrossWeight = dimensionsGrossWeight;
		}

		public String getBatchNumber() {
			return batchNumber;
		}

		public void setBatchNumber(String batchNumber) {
			this.batchNumber = batchNumber;
		}

	}


}
